package com.lx.core;//说明:

/**
 * 创建人:游林夕/2019/4/28 09 04
 */

import com.lx.core.annotation.Bean;
import com.lx.util.LX;

import java.util.*;


@Bean
public class OS{
    @Bean
    private static ApplicationContext applicationContext;
    private static final ThreadLocal<Map<String, Object>> tl = ThreadLocal.withInitial(() -> new HashMap(4));
    public static  <T>T get(String key){
        return (T) tl.get().get(key);
    }
    public static void put(String key, Object obj){
        tl.get().put(key,obj);
    }
    public static void set(Map<String, Object> map){
        tl.set(map);
    }
    public static void remove(){
        tl.remove();
    }

    //说明:设置用户ID
    /**{ ylx } 2020/8/31 16:24 */
    public static void setUserId(String userId){
        put("userId",userId);
    }
    //说明:获取用户ID
    /**{ ylx } 2020/8/31 16:24 */
    public static Long getUserId(){
        return get("userId") == null ? 0 : Long.parseLong(get("userId"));
    }
    /**
     * 通过name获取 Bean.
     */
    public static Object getBean(String name){
        return applicationContext.getBean(name);
    }

    public static <T> List<T> getBean(Class<T> t){
        return applicationContext.getBean(t);
    }

    public static ApplicationContext getApplicationContext(){
        return applicationContext;
    }
    public static String getProperty(String key){
        if (applicationContext == null || applicationContext.getProperties() == null){
            // 从系统配置中获取
            return System.getProperty(key);
        }
        return applicationContext.getProperties().getProperty(key);
    }
    //说明:从配置文件中获取配置, 为null时取默认值
    /**{ ylx } 2020/11/16 9:42 */
    public static String getProperty(String key, String defaultValue){
        String property = getProperty(key);
        return LX.isNotEmpty(property)?property:defaultValue;
    }
    //说明: 获取 int 类型值配置
    /**{ ylx } 2020/12/7 14:55 */
    public static int getIntProperty(String key, String defaultValue){
        return Integer.parseInt(getProperty(key, defaultValue));
    }
    public static long getLongProperty(String key, String defaultValue){
        return Long.parseLong(getProperty(key, defaultValue));
    }
}

